import { type App, nextTick} from 'vue'
import { useTitle } from '@vueuse/core'
import { useSiteConfig } from '../store/siteConfig'
import ElementPlus from 'element-plus'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import { Loading as TLoading } from 'tdesign-vue-next'
import router from "@/router";
import pinia from "@/plugins/store";

export function loadPlugins(app: App) {
  app.use(pinia)
  app.use(router)
  app.use(ElementPlus, {
    locale: zhCn,
  })
  app.use(TLoading)
}

/**
 * 根据路由 meta.title 设置浏览器标题
 */
export function setTitleFromRoute() {
  nextTick(() => {
    if (typeof router.currentRoute.value.meta.title != 'string') {
      return
    }
    const webTitle = router.currentRoute.value.meta.title ? router.currentRoute.value.meta.title : router.currentRoute.value.meta.title
    const title = useTitle()
    const siteConfig = useSiteConfig()
    title.value = `${webTitle}${siteConfig.siteName ? ' - ' + siteConfig.siteName : ''}`
  })
}

export function auth(node: string): boolean
export function auth(node: { name: string; subNodeName?: string }): boolean

/**
 * 鉴权
 * 提供 string 将根据当前路由 path 自动拼接和鉴权，还可以提供路由的 name 对象进行鉴权
 * @param node
 */
export function auth(node: string | { name: string; subNodeName?: string }) {
  // const seller = isAdminApp() ? useNavTabs() : useMemberCenter()
  // if (typeof node === 'string') {
  //   const path = getCurrentRoutePath()
  //   if (seller.state.authNode.has(path)) {
  //     const subNodeName = path + (path == '/' ? '' : '/') + node
  //     if (seller.state.authNode.get(path)!.some((v: string) => v == subNodeName)) {
  //       return true
  //     }
  //   }
  // } else {
  //   // 节点列表中没有找到 name
  //   if (!node.name || !seller.state.authNode.has(node.name)) return false
  //
  //   // 无需继续检查子节点或未找到子节点
  //   if (!node.subNodeName || seller.state.authNode.get(node.name)?.includes(node.subNodeName)) return true
  // }
  return false
}

// 函数重载声明
export function objCopyProperties(source: any, target: any, ignoreProperties?: string[]): void
export function objCopyProperties(source: any, target: any, ignoreNull: boolean): void
export function objCopyProperties(source: any, target: any, ignoreProperties?: string[] | boolean, ignoreNull = false): void {
  // 检查传入的 ignoreProperties 参数是否为布尔值，如果是则将其作为 ignoreNull 的值
  if (typeof ignoreProperties === 'boolean') {
    ignoreNull = ignoreProperties
    ignoreProperties = undefined
  }

  for (const key in source) {
    if (target.hasOwnProperty(key) && (!ignoreProperties || !ignoreProperties.includes(key))) {
      if (ignoreNull && (source[key] === null || source[key] === undefined)) continue
      target[key] = source[key]
    }
  }
}
